Manipulating Data with Matrices

 

Due: February 11th, 2009

 

In computer programming a matrix is a two dimensional array. An array is a list of data values. A matrix, or two dimensional arrays, is a list of values that cross two axis and would appear, conceptually, as a grid. Matrices are used regularly in programming. Computer animation uses matrix and matrix logic extensively.  Matrix multiplication is part of this.

Matrix multiplication can be defined in this way:

In general, we can take the product AB only if the number of columns of A equals the number of rows of B (so that we can multiply the rows of A by the columns of B as above). The product AB is then obtained as follows:

·         To obtain the 1,1 entry of AB, multiply Row 1 of A by Column 1 of B.

·         To obtain the 1,2 entry of AB, multiply Row 1 of A by Column 2 of B.

·         To obtain the 1,3 entry of AB, multiply Row 1 of A by Column 3 of B.
. . .

·         To obtain the 2,1 entry of AB, multiply Row 2 of A by Column 1 of B.

·         To obtain the 2,2 entry of AB, multiply Row 2 of A by Column 1 of B.

It seems quite a complicated procedure at first but after some practice you'll find you get used to it. Here's a diagram to illustrate it.

http://www.ucl.ac.uk/Mathematics/geomath/level2/mat/mat71.gif

and so on. In general,

To obtain the i,j entry of AB, multiply Row i of A by Column j of B.

Note: The product AB has as many rows as A and as many columns as B.

 

You are to write a C++ program that will do the following:

 

1.    Read the input from a data file found here, which contains information on two matrices, one that is a matrix of resources, and one that is a matrix of models. The two matrices come with labels for each column and row.

·         The first line of the file contains a single integer which should be stored in a variable named res_row, the number of rows in the resource matrix.

·         The next line of the file contains a single integer which should be stored in a variable named res_col, the number of columns in the resource matrix

·         The third line contains the names of the individual resources, separated by semicolons. These should be stored in a character array res_headings, one name per row.

·         The fourth line contains the names of the individual models, separated by semicolons. These should be stored in a character array model_headings, one name per row.

·         The next ‘res_row’ lines contain the rows of the resource matrix. These should be stored in a matrix named resource.

·         The next line of the file contains a single integer which should be stored in a variable named model_row, the number of rows in the model matrix.

·         The next line of the file contains a single integer which should be stored in a variable named model_col, the number of columns in the model matrix

·         The next ‘model_row’ lines contain the rows of the model matrix. This should be stored as matrix named model.

·         Your program should be designed to read one set, perform the analysis described below, and then loop back to read another set until all data has been processed. You may assume that the maximum number of resources and models is 10, and that the maximum number of characters in a label is 8. Thus you should #define the constants MAXNUM as 10 and MAXLEN as 9 (which allows for the null at the end of the names).

2.    Output to a file named matrix_output.dat, the resource matrix and the model matrix with neatly labeled row and column headings as illustrated in the example output file found here. This should be done by invoking, for both the resource matrix and the model matrix, a function whose prototype is

 

void tablewrite( int *nrow, int *ncol, double mat[][MAXNUM], char row_label[][9],

      char col_label[][9], char mat_title[30], char row_title[9], ostream &fout);

 

The input arguments are nrow - the number of rows; ncol - the number of columns;  mat - the nrow x ncol matrix; row_label- the character array containing the labels for the rows of the matrix, one label per row; col_label- the character array containing the labels for the columns of the matrix, one label per row; mat_title - a string containing a title for the table; and row_title - a string containing a label to appear over the individual row labels, and a pointer to the output file. 

3.    Invoke a function to multiply the two matrices. You get to write this function with any input/output arguments you want.

4.    Use tablewrite to output the multiplied matrix to the output file

5.    No global variables are allowed!

6.    Make sure that the program works for any size matrix in the range. The test dataset I use to check your program will be completely different.

 

Please make sure that the assignment is handed in, in accordance with the requirements stated in the syllabus.